#include <linux/ioctl.h>


#define DMA_MAX_LENGTH			(512 * 1024)


/**********************************************
*              MCS COMMAND
**********************************************/
enum KMG_Command							//This definition must be synchronized with mcs!!!
{
	KMG_INIT_DEVICE = 0XAABB0000,
	KMG_VERIFICATION,
	KMG_UNVERIFICATION,
	KMG_RESTORE_FACTORYSETTING,
	KMG_GET_DEVICEINFO,
	KMG_INIT_USERINFO,
	KMG_GET_USERINFO,
	KMG_BACKUP_DEVICEKEYPAIR,
	KMG_RESTORE_DEVICEKEYPAIR,
	KMG_BACKUP_PKEY,
	KMG_BACKUP_PKEY_PLAIN,
	KMG_RESTORE_PKEY,
	KMG_CREATE_USRKEYPAIR,
	KMG_BACKUP_USRKEYPAIR,
	KMG_RESTORE_USRKEYPAIR,
	KMG_CREATE_KEKEY,
	KMG_BACKUP_KEKEY,
	KMG_BACKUP_KEKEY_PLAIN,
	KMG_RESTORE_KEKEY,
	KMG_CREATE_SM1KEY,
	KMG_BACKUP_SM1KEY,
	KMG_RESTORE_SM1KEY,
	KMG_RESTORE_SM1KEY_PLAIN,
	KMG_CREATE_SM1AUTH,
	KMG_BACKUP_SM1AUTH,
	KMG_RESTORE_SM1AUTH,
	KMG_COMPARE_SM1AUTH,
	KMG_LOADSM1KEY,
	KMG_LOADFHEPUBKEY,
	KMG_NFHE_SAVEFHEPRIKEY,
	KMG_NFHE_LOADFHEPRIKEY,
	KMG_NFHE_BACKUPFHEPRIKEY,
	KMG_NFHE_RESTOREFHEPRIKEY,
	KMG_UPDATE_KEYMNGERPASSWORD,
	KMG_UPDATE_USRKEYPASSWORD,
	KMG_CHECKKEYINFO,

	DBG_RESTORE_FACTORYSETTING				,
	DBG_EXPORT_PROKEY						,
	DBG_EXPORT_KEKEY						,
	DBG_EXPORT_FHEPRIKEY					,
};

enum SDF_COMMAND
	{
		SDF_OPENDEVICE						=		0XAAAA0201,
		SDF_CLOSEDEVICE									      ,
		SDF_OPENSESSION									      ,
		SDF_CLOSESESSION								      ,
		SDF_GETDEVICEINFO								      ,
		SDF_GENERATERANDOM								      ,
		SDF_GETPRIVATEKEYACCESSRIGHT					      ,
		SDF_RELEASEPRIVATEKEYACCESSRIGHT					  ,

		SDF_EXPORTSIGNPUBLICKEY_ECC			=       0XAAAA0308,
		SDF_EXPORTENCPUBLICKEY_ECC							  ,
		SDF_GENERATEKEYPAIR_ECC							      ,
		SDF_GENERATEKEYWITHIPK_ECC							  ,
		SDF_GENERATEKEYWITHEPK_ECC							  ,
		SDF_IMPORTKEYWITHISK_ECC							  ,
		SDF_GENERATEAGREEMENTDATAWITHECC					  ,
		SDF_GENERATEKEYWITHECC							      ,
		SDF_GENERATEAGREEMENTDATAANDKEYWITHECC 				  ,
		

		SDF_GENERATEKEYWITHKEK				=		0XAAAA0312,
		SDF_IMPORTKEYWITHKEK							      ,
		SDF_IMPORTKEY										  ,
		SDF_DESTORYKEY										  ,
		SDF_CREATE_SM2KEY,
		SDF_BACKUP_SM2KEY,
		SDF_RESTORE_SM2KEY,
		SDF_SM2_ENC,
		SDF_SM2_DEC,


		SDF_EXTERNALSIGN_ECC				=		0XAAAA0405,
		SDF_EXTERNALSIGN_ECC_WITHOUT_KEY					  ,
		SDF_EXTERNALVERIFY_ECC							      ,
		SDF_EXTERNALVERIFY_ECC_WITHOUT_KEY					  ,
		SDF_INTERNALSIGN_ECC							      ,
		SDF_INTERNALVERIFY_ECC							      ,
		SDF_EXTERNALENCRYPT_ECC							      ,
		SDF_EXTERNALDECRYPT_ECC							      ,
		SDF_GENERATERANDOM_CHIP0,
		SDF_GENERATERANDOM_CHIP1,


		SDF_ENCRYPT							=		0XAAAA0501,
		SDF_DECRYPT							                  ,
		SDF_CALCULATEMAC						     	      ,


		SDF_HASHINIT						=		0XAAAA0601,
		SDF_HASHINIT_WITHOUT_KEY							  ,
		SDF_HASHUPDATE										  ,
		SDF_HASHFINAL										  ,


		SDF_CREATEFILE						=		0XAAAA0701,
		SDF_READFILE							              ,
		SDF_WRITEFILE							              ,
		SDF_DELETEFILE										  ,	

		SDF_Test							=		0xBBBB0000,
		SDF_EXPORTPRIVATEKEY_ECC,			/*测试导出私钥*/
		KMG_LOADTHREEKEY					=		0XCCCC0000
	};


/**********************************************
*              Error Define
**********************************************/
#define	NFHE_OK					0x0
enum{
	NFHE_DEVICE_NOTFOUND		=	-1,
	NFHE_CLOSE_FAILED			=	-2,
	NFHE_IOCTL_ERROR			=	-3,
	NFHE_READ_ERROR				=	-4,
	NFHE_WRITE_ERROR			=	-5,
	NFHE_KEYINDEX_INVALID		=   -6,
	NFHE_SM1_BACKUP_ERROR		=	-7,
	NFHE_CHECK_AUTH_ERROR		=	-8,
	NFHE_DEC_TYPE_ERROR			=	-9,
	NFHE_PARAMETERERR			=   -10,
	NFHE_SM3_ERROR				=   -11,
	NFHE_NOT_DEFINED			=	-12,
	NFHE_ERROR_LEN				=	-13,
};

/**********************************************
*              IOCTL struct
**********************************************/
typedef struct decd_ioctl_ctx {
	void *pDataBuf;
	unsigned long inLen;
	unsigned long outLen;
} decd_ioctl_ctx;

typedef struct Write_Usr_ioctl_ctx {
	unsigned int addr;
	unsigned int data;
} Write_Usr_ioctl_ctx;

typedef struct sm1_ioctl_ctx {
	void *inbuf;
	void *outbuf;
	unsigned int inputLength;
	unsigned int outputLength;
	unsigned int ssx_cmd;
} sm1_ioctl_ctx;


typedef struct fhe_ioctl_ctx {
	unsigned int decdata;
	unsigned char encdata[128];
} fhe_ioctl_ctx;


enum {
    INTS32 = 0, 
    INT32 = 40, 
    INTS64 = 1,
	INT64 = 41,
	F32 = 10,
	D64 = 11,
	BIGNUM = 20,
	STR = 30,
};

enum {
	ADD = 0b001,
	SUB = 0b011,
	MUL = 0b101,
	DIV = 0b111,
};

enum {
    COMPARE_LT = 0b10001,
	COMPARE_LTE = 0b10011,
	COMPARE_GT = 0b10101,
	COMPARE_GTE = 0b10111,
	COMPARE_EQ = 0b11001,
	COMPARE_NE = 0b11011,
	LOG_AND = 0b01001,
	LOG_OR = 0b01011,
	LOG_NOT = 0b01101,
	COMPARE_LTP = 0b110001,
	COMPARE_LTEP = 0b110011,
	COMPARE_GTP = 0b110101,
	COMPARE_GTEP = 0b110111,
	COMPARE_EQP = 0b111001,
	COMPARE_NEP = 0b111011,
};


#define IOCTL_RESET						_IO('q', 1)
#define IOCTL_WRITE_USR					_IOWR('q', 2, struct Write_Usr_ioctl_ctx *)
#define IOCTL_GETCARDNUM				_IOWR('q', 3, unsigned char *)
#define IOCTL_FHE_GENKEY				_IO('q', 4)
#define IOCTL_FHE_ENC					_IOWR('q', 5, struct fhe_ioctl_ctx *)
#define IOCTL_FHE_DEC					_IOWR('q', 6, struct fhe_ioctl_ctx *)
#define IOCTL_NFHE_EXPORT_PUBKEY		_IOR('q', 7, unsigned char *)
#define IOCTL_NFHE_IMPORT_PUBKEY		_IOW('q', 8, unsigned char *)
#define IOCTL_NFHE_DMA_SM1				_IOWR('q', 9, struct sm1_ioctl_ctx *)
#define IOCTL_DECD						_IOWR('q', 10, struct decd_ioctl_ctx *)
#define IOCTL_NFHE_TEST					_IOWR('q', 11, struct decd_ioctl_ctx *)
